package com.co.recommend.service.impl;

import com.co.recommend.dao.DetailMapper;
import com.co.recommend.dao.VideoMapper;
import com.co.recommend.dao.VideoStateMapper;
import com.co.recommend.service.PopularVideoService;
import com.co.video.dto.VideoDto;
import com.co.video.pojo.Detail;
import com.co.video.pojo.Video;
import com.co.video.pojo.VideoState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PopularVideoServiceImpl implements PopularVideoService {

    @Autowired
    private DetailMapper detailMapper;
    @Autowired
    private VideoStateMapper videoStateMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Override
    public List<VideoDto> loadPoupVideoState(int w_favo, int w_nowrank, int w_share, int num) throws Exception {

        List<VideoDto> videoDtos = new ArrayList<>();
        //根据点赞、收藏、播放次数状态信息权重加载视频---视频状态信息
        List<VideoState> videoStates = videoStateMapper.loadPoupVideoState(w_favo,w_nowrank,w_share,num);

        if(videoStates ==null || videoStates.size()<=0){
            throw new Exception("当前没有推荐视频可加载....");
        }

        for (VideoState videoState : videoStates) {
            String aid = videoState.getAid();
            //根据视频状态信息aid查询视频总信息
            Detail detail = detailMapper.selectByPrimaryKey(aid);
            //根据aid查询子视频
            Video video = new Video();
            video.setAid(aid);
            List<Video> videos = videoMapper.select(video);
            //添加进DTO返回
            VideoDto videoDto = new VideoDto();
            videoDto.setDetail(detail);
            videoDto.setVideos(videos);
            videoDto.setVideoState(videoState);

            videoDtos.add(videoDto);

        }

        return videoDtos;
    }
}
